﻿#include "MyThread.h"
#include "Global.h"

void RefreshGun::UpdateQRCode()
{
    if(chargingData->cmd6[GUN_NO].req_state == 1)
    {
        GUN_STATE[GUN_NO].m_QrCodeNum = QString(QLatin1String(chargingData->cmd6[GUN_NO].qrCode));
        qDebug()<<"当前的cmd6中的二维码"<<QString(QLatin1String(chargingData->cmd6[GUN_NO].qrCode));
        chargingData->gunmsg[GUN_NO].guntab = GUN_NO + 1;
        chargingData->cmd6[GUN_NO].req_state = 0;
    }
}

void RefreshGun::update_ErrorInfomation()//这个定时器在进入直流数据监控的界面的时候要关掉
{
    if(GUN_STATE[GUN_NO].Page != returnChargeGunPage &&
            GUN_STATE[GUN_NO].Page != chargeWatchingDCPage &&
            GUN_STATE[GUN_NO].Page != settleAccountPage)
    {
        if(chargingData->gunmsg[GUN_NO].errcode[0] != 0 ||/*充电桩的遥信故障*/
                (chargingData->gunmsg[GUN_NO].meter_state > 0) ||/*电表故障*/
                (chargingData->can_state[GUN_NO] > 0) /*||CAN通信故障*/
                /*(card->errcode > 0)读卡器的故障*/)
        {
            GUN_STATE[GUN_NO].Page = equipmentMaintaincePage;//故障界面
            swipCardflag = 0;//一旦故障需要返回未刷卡的状态
            swipcard_YCT = 0;
            VINcharge = 0;
            emit Device_is_Error(GUN_NO);
        }
        else if(chargingData->gunmsg[GUN_NO].gun_conn_state != 0 /*充电枪非连接*/)
        {
            GUN_STATE[GUN_NO].Page = chargeGunNotConnectPage;
            swipCardflag = 0;//被拔枪后需要返回未刷卡的状态
            swipcard_YCT = 0;
            VINcharge = 0;
            putInChargerFlag[GUN_NO] = 0;
            emit Device_is_NotConnect(GUN_NO);
        }
        else if(chargingData->gunmsg[GUN_NO].gun_conn_state == 0 &&/*充电枪已连接*/
                chargingData->gunmsg[GUN_NO].charge_state != 1 &&/*不处于正在充电的状态*/
                swipCardflag == 0 && swipcard_YCT == 0 && VINcharge == 0)
        {
            // jump to chooseGun Page, send once!
            if ((putInChargerFlag[GUN_NO] == 0) && GUN_NO < chargingData->gunnum)
            {
                GUN_STATE[GUN_NO].Page = cardAndQRCodeVerifyPage;
                putInChargerFlag[GUN_NO] = 1;
                emit putInCharger(GUN_NO);
            }
        }
    }
}
void RefreshGun::ChargingInfomation_Monitoring(int _focus_gun_)
{
    if(_focus_gun_-1 == GUN_NO)
    {
        //开始充电监控...
        qDebug("开始充电监控...\n");
        ErrorTimer->stop();
        Charging_Monitoring.start(500);
    }
}
void RefreshGun::Moitoring_feedback()
{
    qDebug("%d号枪充电监控\n",GUN_NO+1);
    if((chargingData->gunmsg[GUN_NO].charge_state == 2) || (chargingData->gunmsg[GUN_NO].charge_state == 4))
    {
        if(swipcard_YCT != 1)
        {
            //充电结束信号
            emit StopingCharging(GUN_NO);
            Charging_Monitoring.stop();
            qDebug()<<">>>>>>>>>>>>>>>发出充电结束的信号<<<<<<<<<<<<<<<<<<<<";
        }
        else
        {
            emit ChargeOver_YCT(GUN_NO);//羊城通充电结束
        }
    }

    if(chargingData->gunmsg[GUN_NO].u2.dc.MX[2] > 0)//充电目标车的剩余电量
    {
        charge_status = 1;
    }

    qDebug("charge_state%d,MX%d,charge_status%d\n",chargingData->gunmsg[GUN_NO].charge_state,
           chargingData->gunmsg[GUN_NO].u2.dc.MX[2],charge_status);
    if(((chargingData->gunmsg[GUN_NO].charge_state == 0) || (chargingData->gunmsg[GUN_NO].charge_state == 1)) &&
            (chargingData->gunmsg[GUN_NO].u2.dc.MX[2] == 0) &&
            (charge_status == 0))
    {
         if(chargingData->cmd2[GUN_NO].result == 2)
         {
             emit onStartingDeviceFail(GUN_NO);//启动失败
             qDebug()<<">>>>>>>发出启动失败的信号<<<<<<<<<<<";
         }
         else
         {
             //正在启动
             emit onStartingDevice(GUN_NO);
             qDebug()<<">>>>>>>>>>>>>>>发出正在启动的信号<<<<<<<<<<<<<<<";
         }
    }
    else if((chargingData->gunmsg[GUN_NO].charge_state == 1) &&
            (chargingData->gunmsg[GUN_NO].u2.dc.MX[2] > 0) &&
            (charge_status == 1))
    {
        //正在充电
        emit onCharging(GUN_NO);//正在充电
        qDebug()<<">>>>>>>>>>>>>>>>>>>>>>>>>发出正在充电的信号<<<<<<<<<<<<<<<<<<";
        chargingData->cmd2[GUN_NO].state = 0;
        chargingData->cmd2[GUN_NO].check_result = 0;
//         onStopChargeHint(m_num);
    }

    if(card->errcode != 0 &&
           chargingData->cmd3[GUN_NO].state == 0 &&
           chargingData->gunmsg[GUN_NO].errcode[0] == 0 &&
           charge_status == 1)//在刷卡启动时检测到读卡器故障
    {
        chargingData->cmd4[GUN_NO].guntab = GUN_NO + 1;
        chargingData->cmd4[GUN_NO].req_state = 1;//充电结束请求
        chargingData->cmd4[GUN_NO].stopReason = 1;
        protocol_exit_seek_card_status();
    }
}

void RefreshGun::ReceiveSwipsignal(QString cardnum, int _focus_gun_)
{
    if(_focus_gun_-1 == GUN_NO)
    {
        swipCardflag = 1;//刷卡标志位
        ThisGun_SwipCard_Num = cardnum;//在刷卡停止充电的时候会使用到
        GUN_STATE[GUN_NO].Page = chargeTypePage;

        chargingData->cmd2[GUN_NO].check_result = 1;
        qDebug("卡号是: ");
        for(int i=0; i<cardnum.size(); i++)
        {
            chargingData->cmd2[GUN_NO].cardNUM[i] =  cardnum.at(i).toAscii();
            printf(" %d ", chargingData->cmd2[GUN_NO].cardNUM[i]);
        }
        printf("\n");
        chargingData->cmd2[GUN_NO].available_money = 100000;
    }
}
